//
//  MMGCustomNavigationBar.m
//  DollsVending
//
//  Created by Don on 2017/11/1.
//  Copyright © 2017年 Memego, Inc. All rights reserved.
//

#import "MMGCustomNavigationBar.h"
#import "UIImage+Color2Image.h"
#import "UIBarButtonItem+BlocksKit.h"
#import "UIImage+CacheViaName.h"

static float const kRJ_CustomNavigationBarHeight = 64;
@interface MMGCustomNavigationBar ()
@property (nonatomic, strong) UIView *navigationBarStatusBarContainer;
@property (nonatomic, strong) UIToolbar *customNavigationBar;
@property (nonatomic, assign) CGFloat customNavigationBarStatusBarHeight;

@property (nonatomic, strong) RACSubject *leftItemSignal;
@end
@implementation MMGCustomNavigationBar

- (instancetype)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self setUI];
    }
    return self;
}

#pragma mark - public
- (void)mmg_setTranslucentNavigationBarWithLeftItemImage:(NSString *)leftItemImage title:(NSString *)title rightBarButtonItems:(NSArray<UIBarButtonItem *> *)items {
    
    UIToolbar *toolBar = [self toolBarWithBackgroudColor:[UIColor clearColor] tintColor:nil];
    [self addSubview:toolBar];
    UIBarButtonItem *backItem = [self backBarButtonItemWithImage:leftItemImage];
    UIBarButtonItem *spaceItem = [self spaceItem];
    UIBarButtonItem *titleItem = [self titleItemWithTitle:title];
    if ([title hasSuffix:@".png"]||[title hasSuffix:@".jpg"]) {
        titleItem = [self titleItemWithImageName:title];
    }
    
    NSMutableArray *itemArray = nil;
    if (items.count > 0 && title.length > 0) {
        itemArray = [NSMutableArray arrayWithArray:@[backItem, spaceItem, titleItem, spaceItem]];
        [itemArray addObjectsFromArray:items];
    } else if (title.length > 0) {
        itemArray = [NSMutableArray arrayWithArray:@[backItem, spaceItem, titleItem, spaceItem]];
    } else if (items.count > 0) {
        itemArray = [NSMutableArray arrayWithArray:@[backItem, spaceItem]];
        [itemArray addObjectsFromArray:items];
    } else {
        itemArray = [NSMutableArray arrayWithObject:backItem];
    }
    [toolBar setItems:itemArray animated:YES];
    
}

/**
 *  隐藏导航栏（只有title）
 */
- (void)mmg_setNavigationBarTransparentOnlyWithTitle:(NSString *)title tintColor:(UIColor *)tintColor {
    [self mmg_setNavigationBarColor:[UIColor clearColor] title:title tintColor:tintColor];
}

- (void)mmg_setNavigationBarColor:(UIColor *)navBarColor title:(NSString *)title tintColor:(UIColor *)tintColor {
    UIBarButtonItem *spaceItem = [self spaceItem];
    UIBarButtonItem *titleItem = [self titleItemWithTitle:title];
    if ([title hasSuffix:@".png"]||[title hasSuffix:@".jpg"]) {
        titleItem = [self titleItemWithImageName:title];
    }
    UIToolbar *toobar = [self toolBarWithBackgroudColor:navBarColor tintColor:tintColor];
    [toobar setItems:@[spaceItem, titleItem, spaceItem] animated:YES];
}


#pragma mark - private
- (void)setUI {
    self.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), self.customNavigationBarStatusBarHeight);
}

- (UIToolbar *)toolBarWithBackgroudColor:(UIColor *)bgColor tintColor:(UIColor *)tintColor {
    UIToolbar *toolBar = self.customNavigationBar;
    toolBar.superview.backgroundColor = bgColor;
    toolBar.translucent = YES;
    if (tintColor) {
        toolBar.tintColor = tintColor;
    }
    
    [toolBar setBackgroundImage:[UIImage mmg_imageWithColor:bgColor] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [toolBar setShadowImage:[UIImage mmg_imageWithColor:bgColor] forToolbarPosition:UIBarPositionAny];
    return toolBar;
}

- (UIBarButtonItem *)backBarButtonItemWithImage:(NSString *)imageName {
    UIImage *image = nil;
    if (imageName) {
        image = [UIImage imageNamed:imageName];
    }
    
    MMGWeakSelf(weakSelf);
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:image forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, 38, 38);
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] bk_initWithCustomView:backButton];
    [[backButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [weakSelf.leftItemSignal sendNext:x];
    }];
    if (!imageName) {
        backItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return backItem;
}

- (UIBarButtonItem *)spaceItem {
    return [[UIBarButtonItem alloc] bk_initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace handler:nil];
}

- (UIBarButtonItem *)titleItemWithTitle:(NSString *)title {
    return [[UIBarButtonItem alloc] bk_initWithTitle:title style:UIBarButtonItemStylePlain handler:nil];
}

- (UIBarButtonItem *)titleItemWithImageName:(NSString *)titleImg {
    UIImageView *imv = [[UIImageView alloc] initWithImage:[UIImage mmg_imageNamed:titleImg]];
    return [[UIBarButtonItem alloc] bk_initWithCustomView:imv];
}

- (CGFloat)customNavigationBarStatusBarHeight {
    CGFloat statusBarHeight = CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]);
    
    return (statusBarHeight*0.5) + kRJ_CustomNavigationBarHeight;
}

#pragma mark - lazy init
- (UIToolbar *)customNavigationBar {
    if (!_customNavigationBar) {
        CGFloat statusBarHeight = CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]);
        _customNavigationBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, (statusBarHeight)*0.5, [UIScreen mainScreen].bounds.size.width, kRJ_CustomNavigationBarHeight)];
        
        [self.navigationBarStatusBarContainer addSubview:_customNavigationBar];
    }
    
    return _customNavigationBar;
    
}

- (UIView *)navigationBarStatusBarContainer {
    
    if (!_navigationBarStatusBarContainer) {
        _navigationBarStatusBarContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.customNavigationBarStatusBarHeight)];
        [self addSubview:_navigationBarStatusBarContainer];
    }
    
    return _navigationBarStatusBarContainer;
    
}

- (RACSubject *)leftItemSignal {
    if (!_leftItemSignal) {
        _leftItemSignal = [RACSubject subject];
    }
    
    return _leftItemSignal;
}

@end
